#include "string.h"
#include "main.h"
#include "LsdMain.h"
#include "LsdPublicUse.h"
#include "LsdUARTBase.h"
#include "LsdIOTTask.h"
#include "LsdMem.h"
#include "bsp_usart.h"
#include "LsdIOT_AT.h"
#include "LsdEEPROM.h"
#include "base64.h"
#include "LsdAES.h"
#include "LsdAPP.h"
#include "LsdWaterKeyTask.h"
#include "LsdScreenTask.h"
#include "bsp_rtc.h"
#include "LsdPublicUse.h"
#include "LsdCtrlTask.h"
#include "main.h"
#include "LsdOTA.h"
TypeLinkedList CTRLDataWrite06List={NULL, NULL, 0};  //@7 添加IOT数据链表  就是属性表没有包装的数据   
TypeLinkedList CTRLDataWrite10List={NULL, NULL, 0};  //@7 添加IOT数据链表  就是属性表没有包装的数据   
TypeLinkedList CTRLDataWrite30List={NULL, NULL, 0};  //@7 添加OTA升级链表  就是属性表没有包装的数据   

typedef struct{
	uint8_t Index;//索引
	uint32_t basic_addr;//基地址
	uint32_t addr_offset;//地址偏移
	uint16_t write_len;//读长度
	uint8_t  busy;//是否在发送 0:不在发送状态  1:发送状态
}modData;
modData mdData = {0};

void MODH_Send03H(uint8_t _addr, uint16_t _reg, uint16_t _num)
{
	TypeBuffDef send;
	uint16_t index = 0;
	send.buff = pvPortMalloc(128);

	send.buff[index++] 	= _addr;		/* 从站地址 */
	send.buff[index++]  = 0x03;		/* 功能码 */	
	send.buff[index++] 	= _reg >> 8;	/* 寄存器编号 高字节 */
	send.buff[index++]  = _reg;		/* 寄存器编号 低字节 */
	send.buff[index++]  = _num >> 8;	/* 寄存器个数 高字节 */
	send.buff[index++]  = _num;		/* 寄存器个数 低字节 */
	
	uint16_t crc16 = CRC16_Modbus(&send.buff[0], index);//crc校验
  send.buff[index++] = (crc16 >> 8) & 0xFF;
  send.buff[index++] = crc16 & 0xFF;
	send.len = index;
	
//	for(uint16_t i=0;i<send.len;i++)
//	printf("%02x",send.buff[i]);
//	printf("\r\n");
//	
	OnUARTSendData(UART_PORT_TAP, send.buff, send.len);//发送数据
	vPortFree(send.buff);
}

void MODH_Read_03H(uint8_t *tbuff, uint16_t tlen)
{
	static tuMainErrStatus last_alarm;
	uint8_t bytes;
	
	last_alarm = powerDownSave.alarm;
//	uint8_t *p;
	if (tlen > 0)
	{
		bytes = tbuff[2];	/* 数据长度 字节数 */	
		uint8_t *p = &tbuff[3];//数据
//		uint16_t len = 0;
		uint8_t *q = NULL;
		if(tbuff[2] != (mdData.write_len*2))return;
		switch(mdData.basic_addr)
		{
			case 0x1000:
			{
				if((mdData.addr_offset+ mdData.write_len*2) > sizeof(TypeChipInfo)){print_debug("TypeChipInfo modebus over len\r\n");  return;}//超出长度报错
				q = (uint8_t*)(&gROCtrlInfo.softVer + mdData.addr_offset);
				break;
			}
			case 0x2000:
			{	
				if((mdData.addr_offset+ mdData.write_len*2) > sizeof(tsPowerDownSave)){print_debug("modebus over len\r\n");  return;}//超出长度报错
				q = (uint8_t*)(&powerDownSave.magic[0] + mdData.addr_offset);
				break;
			}
			case 0x3000:
			{
				if((mdData.addr_offset+ mdData.write_len*2) > (sizeof(tsMasterUIInfo)-sizeof(DevMasterUIInfo.save_magic))){print_debug("DevMasterUIInfo over len\r\n");  return;}//超出长度报错
				q = (uint8_t*)(&DevMasterUIInfo.heat_temp_set + mdData.addr_offset);
				break;
			}		
			case 0x4000:
			{
				if((mdData.addr_offset+ mdData.write_len*2) > sizeof(tsDisPlayInfo)){print_debug("DisPlayInfo over len\r\n");  return;}//超出长度报错
				q = (uint8_t*)(&DevDisPlayInfo.key_status + mdData.addr_offset);
				break;
			}				

		}
		while(bytes && (q != NULL))
		{
			*(q+1) = *p;
			*q		 = *(p+1);
			q += 2;
			p += 2;
			bytes -= 2;
		}
			//故障告警以那边为主
	}
	powerDownSave.alarm.value = ((powerDownSave.alarm.value & (~0x7CDF9480))	| (last_alarm.value & (0x7CDF9480)));//保存该保存的数据 以哪个为主
}

void MODH_Send06H(uint8_t _addr, uint16_t _reg, uint16_t _value)
{
	TypeBuffDef send;
	uint16_t index = 0;
	send.buff = pvPortMalloc(128);

	send.buff[index++] = _addr;			/* 从站地址 */
	send.buff[index++] = 0x06;			/* 功能码 */	
	send.buff[index++] = _reg >> 8;		/* 寄存器编号 高字节 */
	send.buff[index++] = _reg;			/* 寄存器编号 低字节 */
	send.buff[index++] = _value >> 8;		/* 寄存器值 高字节 */
	send.buff[index++] = _value;			/* 寄存器值 低字节 */
	
	uint16_t crc16 = CRC16_Modbus(&send.buff[0], index);//crc校验
  send.buff[index++] = (crc16 >> 8) & 0xFF;
  send.buff[index++] = crc16 & 0xFF;
	send.len = index;
	OnUARTSendData(UART_PORT_TAP, send.buff, send.len);//发送数据
	vPortFree(send.buff);

}
static void MODH_Read_06H(uint8_t *tbuff, uint16_t tlen)
{
	if (tlen > 0)
	{
		if (tbuff[0] == SLAVE_ADDR)		
		{
//			g_tModH.fAck06H = 1;		/* 接收到应答 */
			
		}
	}
}
void MODH_Send10H(uint8_t _addr, uint16_t _reg, uint8_t _num, uint8_t *_buf)
{
	TypeBuffDef send;
	uint16_t index = 0;
	send.buff = pvPortMalloc(128);
	
	send.buff[index++] = _addr;				/* 从站地址 */
	send.buff[index++] = 0x10;				/* 功能码 */	
	send.buff[index++] = _reg >> 8;		/* 寄存器编号 高字节 */
	send.buff[index++] = _reg;				/* 寄存器编号 低字节 */
	send.buff[index++] = _num >> 8;		/* 寄存器个数 高字节 */
	send.buff[index++] = _num;				/* 寄存器个数 低字节 */
	send.buff[index++] = 2 * _num;		/* 数据字节数 */
	
	memcpy(&send.buff[index],_buf,_num*2);
	index += _num*2;
	uint16_t crc16 = CRC16_Modbus(&send.buff[0], index);//crc校验
  send.buff[index++] = (crc16 >> 8) & 0xFF;
  send.buff[index++] = crc16 & 0xFF;
	send.len = index;
//	for(uint16_t i=0;i<send.len;i++)
//	printf("%02x",send.buff[i]);
//	printf("\r\n");
	
	
	OnUARTSendData(UART_PORT_TAP, send.buff, send.len);//发送数据
	vPortFree(send.buff);

}
static void MODH_Read_30H(uint8_t *tbuff, uint16_t tlen)//ota升级用
{
		uint16_t 	index 	= 0;
		uint16_t 	step 	= 0;
	  uint16_t 	cmdLen	=	0; //长度
		uint8_t 	cmdSer	=	0; //序号
		uint8_t 	cmd			=	0;//命令
		uint8_t 	type		=	0;//类型
//		uint8_t   sumCrc=33; //初值
	  uint8_t  *buff = pvPortMalloc(2048); 
		memset(buff,0,2048);
		while(index < tlen)
		{
			switch(step)
			{
				case 0:   if(tbuff[index++]	==	0x55) 													step++; 					break;
				case 1: 	if(tbuff[index++]	==	0xaa) 													step++; 					break;
				case 2: 	cmdLen 						= 	tbuff[index++];									step++; 	 				break;   //数据的长度
				case 3:   cmdLen 		 				+= (tbuff[index++]<<8);	cmdLen -=6;	step++;						break;
				case 4: 	cmd 			  			= 	tbuff[index++];									step++; 					break;  //命令
				case 5:		type							= 	tbuff[index++];									step++;						break;	//类型
				case 6:   memcpy(buff,&tbuff[index],cmdLen); index = tlen;			step++; 							
				//得到一帧有效的命令数据
				
//				OnCockDownLoadReq(buff, cmdLen);
				
				if(mdData.Index == 4)			mdData.Index = 3;
				else if(mdData.Index == 3)mdData.Index = 4;
				break; 	//数据赋值
				default:  break;
			}
		}
		vPortFree(buff);
}
//发送数据给控制板
uint8_t MODH_WriteParam_06H(uint16_t _reg, uint16_t _value)
{
		TypeBuffDef send;
//		uint16_t index = 0;
		send.buff = pvPortMalloc(128);
		send.len = 0;
		send.buff[send.len++] = _reg >> 8;	/* 寄存器编号 高字节 */
		send.buff[send.len++] = _reg;				/* 寄存器编号 低字节 */
		send.buff[send.len++] = _value >> 8;/* 寄存器值 高字节 */
		send.buff[send.len++] = _value;			/* 寄存器值 低字节 */
		
		OnAddBuffsToList(&CTRLDataWrite06List, (uint8_t*)send.buff, send.len); //添加到链表
		vPortFree(send.buff);
	
		return 0;
}
//发送数据给控制板
uint8_t MODH_WriteParam_10H(uint16_t _reg, uint8_t _num, uint8_t *_buf,uint16_t _len)
{
		TypeBuffDef send;
//		uint16_t index = 0;
		send.buff = pvPortMalloc(128);
		send.len = 0;
		send.buff[send.len++] = _reg >> 8;	/* 寄存器编号 高字节 */
		send.buff[send.len++] = _reg;				/* 寄存器编号 低字节 */
		send.buff[send.len++] = _num >> 8;	/* 寄存器个数 高字节 */
		send.buff[send.len++] = _num;				/* 寄存器个数 低字节 */
		uint16_t bytes = _num*2;	/* 数据长度 字节数 */
		if(_num*2>_len)return 0;//防止越界
	
		uint8_t *q = &send.buff[send.len];
	  uint8_t *p = _buf;
		while(bytes)
		{
			*(q+1) = *p;
			*q		 = *(p+1);
			q += 2;
			p += 2;
			bytes -= 2;
		}
		send.len += _len;
		for(uint16_t i=0;i<send.len;i++)
		printf("%02x",send.buff[i]);
		printf("\r\n");
		OnAddBuffsToList(&CTRLDataWrite10List, (uint8_t*)send.buff, send.len); //添加到链表
		vPortFree(send.buff);
		return 0;
}


void MODH_WriteParam_30H(uint8_t _addr, uint8_t _num, uint8_t *_buf)//ota升级
{
	TypeBuffDef send;
	uint16_t index = 0;
	send.buff = pvPortMalloc(2048);
	
	send.buff[index++] = _addr;				/* 从站地址 */
	send.buff[index++] = 	0X30;					/* 功能码 */	
	memcpy(&send.buff[index],_buf,_num);
	index += _num;
	uint16_t crc16 = CRC16_Modbus(&send.buff[0], index);//crc校验
  send.buff[index++] = (crc16 >> 8) & 0xFF;
  send.buff[index++] = crc16 & 0xFF;
	send.len = index;
	printf("MODH_WriteParam_30H  send*********\r\n");
	for(uint16_t i=0;i<send.len;i++)
	printf("%02x",send.buff[i]);
	printf("\r\n");
	
	OnUARTSendData(UART_PORT_TAP, send.buff, send.len);//发送数据
	vPortFree(send.buff);
}

static void OnModebusAnalysis(uint8_t *tbuff, uint16_t tlen)
{
	typedef union 
	{
		uint16_t crc16;
		struct {
				uint8_t	H;
				uint8_t	L;
		}crc;	
	}tucrc;
	tucrc crcBuf;
	if((tbuff != NULL) && (tlen >= 0x06))
	{
		crcBuf.crc16 = CRC16_Modbus(tbuff, tlen-2);//crc校验
		if(crcBuf.crc.H == tbuff[tlen-1] && (crcBuf.crc.L == tbuff[tlen-2]))//校验合格 01 03 //地址20 00 //数量00 0a ce 0d
		{
			switch (tbuff[1])			/* 第2个字节 功能码 */
			{
				case 0x03:	/* 读取保持寄存器 在一个或多个保持寄存器中取得当前的二进制值 */
				{
					MODH_Read_03H(tbuff,tlen);
//					mdData.Index++;
					mdData.busy = 0;
					break;
				}
				case 0x06:
				{
					MODH_Read_06H(tbuff,tlen);
					mdData.Index++;
					mdData.busy = 0;
					break;
				}
				case 0x10:
				{
//					MODH_Read_06H(tbuff,tlen);
//					mdData.Index++;
					mdData.busy = 0;
					break;
				}
				case 0x30://30
				{
					printf("MODH_Read_30H  read*********\r\n");
					for(uint16_t i=0;i<tlen;i++)
					printf("%02x",tbuff[i]);
					printf("\r\n");
					MODH_Read_30H(tbuff,tlen);
					mdData.busy = 0;
					gLoabal.OTASlaveDelay = 0;
					break;
				}
				default:
					break;
			}
		}
	}
}










